<?php
/**
 * Unified Admin Dashboard
 * Manages: Products, Users, Orders in one interface
 */
session_start();
require_once '../config/database.php';
require_once '../includes/functions.php';
requireAdmin();

$section = $_GET['section'] ?? 'dashboard';
$page_title = "Admin Dashboard";

include '../includes/header.php';
?>

<style>
.admin-nav {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 2rem;
    border-bottom: 2px solid #e0e0e0;
    flex-wrap: wrap;
}
.admin-nav a {
    padding: 1rem 1.5rem;
    text-decoration: none;
    color: #666;
    border-bottom: 3px solid transparent;
    transition: all 0.3s;
    font-weight: 500;
}
.admin-nav a.active {
    color: #e74c3c;
    border-bottom-color: #e74c3c;
}
.admin-nav a:hover {
    color: #e74c3c;
    border-bottom-color: #e74c3c;
}
</style>

<main style="padding: 2rem 0;">
    <div class="container">
        <h1 style="margin-bottom: 1rem;">Admin Dashboard</h1>
        
        <nav class="admin-nav">
            <a href="?section=dashboard" class="<?php echo $section == 'dashboard' ? 'active' : ''; ?>">
                <i class="fas fa-home"></i> Dashboard
            </a>
            <a href="?section=products" class="<?php echo $section == 'products' ? 'active' : ''; ?>">
                <i class="fas fa-box"></i> Products
            </a>
            <a href="?section=users" class="<?php echo $section == 'users' ? 'active' : ''; ?>">
                <i class="fas fa-users"></i> Users
            </a>
            <a href="?section=orders" class="<?php echo $section == 'orders' ? 'active' : ''; ?>">
                <i class="fas fa-shopping-bag"></i> Orders
            </a>
        </nav>

        <?php
        // Handle different sections
        switch($section) {
            case 'products':
                // Products management section
                $products = getAllProducts($pdo, 100);
                ?>
                <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 2rem;">
                    <h2 style="color: #2c3e50;">Manage Products</h2>
                    <button onclick="showProductModal()" class="btn btn-primary">
                        <i class="fas fa-plus"></i> Add New Product
                    </button>
                </div>
                
                <?php if (isset($_GET['success'])): ?>
                    <div class="alert alert-success">
                        <i class="fas fa-check-circle"></i> Product added successfully!
                    </div>
                <?php endif; ?>
                
                <div class="card">
                    <div class="card-header"><h2>All Products</h2></div>
                    <?php if (empty($products)): ?>
                        <div style="text-align: center; padding: 3rem; color: #7f8c8d;">
                            <i class="fas fa-box" style="font-size: 3rem; margin-bottom: 1rem;"></i>
                            <p>No products found. <button onclick="showProductModal()" class="btn btn-primary">Add your first product</button></p>
                        </div>
                    <?php else: ?>
                        <div class="table-responsive">
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th>Image</th><th>Name</th><th>Price</th><th>Stock</th><th>Featured</th><th>Status</th><th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($products as $product): ?>
                                    <tr>
                                        <td>
                                            <?php
                                            $product_images = [
                                                'Serenity Candle' => 'assets/images/Serenity Candle.jpg',
                                                'Velvet Oud Diffuser' => 'assets/images/Velvet Oud Diffuser.jpg',
                                                'Monthly Aura Box' => 'assets/images/Monthly Aura Box.jpg',
                                                'Ocean Breeze Candle' => 'assets/images/Ocean Breeze Candle.jpg',
                                                'Sandalwood Essential Oil' => 'assets/images/Sandalwood Essential Oil.jpg',
                                                'Luxury Gift Set' => 'assets/images/Luxury Gift Set.jpg'
                                            ];
                                            $image_file = $product['image_url'];
                                            if (!$image_file && isset($product_images[$product['name']])) {
                                                $image_file = $product_images[$product['name']];
                                            }
                                            $image_url = $image_file ? '/chuan/535g/' . $image_file : '';
                                            ?>
                                            <?php if ($image_url): ?>
                                                <img src="<?php echo htmlspecialchars($image_url); ?>" alt="<?php echo htmlspecialchars($product['name']); ?>" style="width: 50px; height: 50px; object-fit: cover; border-radius: 4px;">
                                            <?php else: ?>
                                                <div style="width: 50px; height: 50px; background: #f8f9fa; border-radius: 4px; display: flex; align-items: center; justify-content: center;">
                                                    <i class="fas fa-image" style="color: #7f8c8d;"></i>
                                                </div>
                                            <?php endif; ?>
                                        </td>
                                        <td>
                                            <strong><?php echo htmlspecialchars($product['name']); ?></strong>
                                            <br><small style="color: #7f8c8d;"><?php echo htmlspecialchars(substr($product['description'], 0, 50)) . '...'; ?></small>
                                        </td>
                                        <td><?php echo formatPrice($product['price']); ?></td>
                                        <td>
                                            <span class="status-badge <?php echo $product['stock_quantity'] > 0 ? 'status-active' : 'status-inactive'; ?>">
                                                <?php echo $product['stock_quantity']; ?>
                                            </span>
                                        </td>
                                        <td>
                                            <span class="status-badge <?php echo $product['is_featured'] ? 'status-active' : 'status-inactive'; ?>">
                                                <?php echo $product['is_featured'] ? 'Featured' : 'Regular'; ?>
                                            </span>
                                        </td>
                                        <td>
                                            <span class="status-badge <?php echo $product['is_active'] ? 'status-active' : 'status-inactive'; ?>">
                                                <?php echo $product['is_active'] ? 'Active' : 'Inactive'; ?>
                                            </span>
                                        </td>
                                        <td>
                                            <div style="display: flex; gap: 0.5rem;">
                                                <a href="../products/view.php?id=<?php echo $product['id']; ?>" target="_blank" class="btn btn-sm btn-outline" title="View">
                                                    <i class="fas fa-eye"></i>
                                                </a>
                                                <button onclick="editProduct(<?php echo $product['id']; ?>)" class="btn btn-sm btn-outline" title="Edit">
                                                    <i class="fas fa-edit"></i>
                                                </button>
                                                <button onclick="deleteProduct(<?php echo $product['id']; ?>)" class="btn btn-sm btn-outline" style="color: #e74c3c;" title="Delete">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </div>
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endif; ?>
                </div>
                <?php
                break;
                
            case 'users':
                // Users management section
                $stmt = $pdo->query("SELECT * FROM users ORDER BY created_at DESC");
                $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
                ?>
                <h2 style="color: #2c3e50; margin-bottom: 2rem;">User Management</h2>
                <div class="card">
                    <div class="card-header"><h2>All Users</h2></div>
                    <?php if (empty($users)): ?>
                        <div style="text-align: center; padding: 3rem; color: #7f8c8d;">
                            <i class="fas fa-users" style="font-size: 3rem; margin-bottom: 1rem;"></i>
                            <p>No users found.</p>
                        </div>
                    <?php else: ?>
                        <div class="table-responsive">
                            <table class="table">
                                <thead>
                                    <tr><th>Name</th><th>Email</th><th>Role</th><th>Status</th><th>Joined</th><th>Actions</th></tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($users as $user): ?>
                                    <tr>
                                        <td><strong><?php echo htmlspecialchars($user['first_name'] . ' ' . $user['last_name']); ?></strong></td>
                                        <td><?php echo htmlspecialchars($user['email']); ?></td>
                                        <td>
                                            <span class="status-badge <?php echo $user['role'] === 'admin' ? 'status-active' : 'status-inactive'; ?>">
                                                <?php echo ucfirst($user['role']); ?>
                                            </span>
                                        </td>
                                        <td>
                                            <span class="status-badge <?php echo $user['email_verified'] ? 'status-active' : 'status-inactive'; ?>">
                                                <?php echo $user['email_verified'] ? 'Verified' : 'Unverified'; ?>
                                            </span>
                                        </td>
                                        <td><?php echo date('M j, Y', strtotime($user['created_at'])); ?></td>
                                        <td>
                                            <div style="display: flex; gap: 0.5rem;">
                                                <button onclick="editUser(<?php echo $user['id']; ?>)" class="btn btn-sm btn-outline" title="Edit">
                                                    <i class="fas fa-edit"></i>
                                                </button>
                                                <?php if ($user['role'] !== 'admin'): ?>
                                                <button onclick="deleteUser(<?php echo $user['id']; ?>)" class="btn btn-sm btn-outline" style="color: #e74c3c;" title="Delete">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                                <?php endif; ?>
                                            </div>
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endif; ?>
                </div>
                <?php
                break;
                
            case 'orders':
                // Orders management section
                $orders = getAllOrders($pdo, 50);
                ?>
                <h2 style="color: #2c3e50; margin-bottom: 2rem;">Order Management</h2>
                <div class="card">
                    <div class="card-header"><h2>All Orders</h2></div>
                    <?php if (empty($orders)): ?>
                        <div style="text-align: center; padding: 3rem; color: #7f8c8d;">
                            <i class="fas fa-shopping-bag" style="font-size: 3rem; margin-bottom: 1rem;"></i>
                            <p>No orders found yet.</p>
                        </div>
                    <?php else: ?>
                        <div class="table-responsive">
                            <table class="table">
                                <thead>
                                    <tr><th>Order #</th><th>Customer</th><th>Email</th><th>Total</th><th>Status</th><th>Date</th><th>Actions</th></tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($orders as $order): ?>
                                    <tr>
                                        <td>
                                            <a href="order_details.php?id=<?php echo $order['id']; ?>" style="color: #e74c3c; font-weight: 500;">
                                                <?php echo htmlspecialchars($order['order_number']); ?>
                                            </a>
                                        </td>
                                        <td><?php echo htmlspecialchars($order['first_name'] . ' ' . $order['last_name']); ?></td>
                                        <td><?php echo htmlspecialchars($order['email']); ?></td>
                                        <td><?php echo formatPrice($order['total_amount']); ?></td>
                                        <td>
                                            <span class="status-badge status-<?php echo $order['status']; ?>">
                                                <?php echo ucfirst($order['status']); ?>
                                            </span>
                                        </td>
                                        <td><?php echo date('M j, Y', strtotime($order['created_at'])); ?></td>
                                        <td>
                                            <div style="display: flex; gap: 0.5rem;">
                                                <a href="order_details.php?id=<?php echo $order['id']; ?>" class="btn btn-sm btn-outline" title="View Details">
                                                    <i class="fas fa-eye"></i>
                                                </a>
                                                <button onclick="updateOrderStatus(<?php echo $order['id']; ?>)" class="btn btn-sm btn-outline" title="Update Status">
                                                    <i class="fas fa-edit"></i>
                                                </button>
                                            </div>
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endif; ?>
                </div>
                <?php
                break;
                
            default:
                // Dashboard section
                $revenue_stats = getRevenueStats($pdo);
                $customer_stats = getCustomerStats($pdo);
                $top_products = getTopProducts($pdo, 5);
                $recent_orders = getAllOrders($pdo, 10);
                ?>
                <!-- Stats Overview -->
                <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 1.5rem; margin-bottom: 3rem;">
                    <div class="card">
                        <div style="display: flex; align-items: center; gap: 1rem;">
                            <div style="background: #e74c3c; color: white; width: 60px; height: 60px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 1.5rem;">
                                <i class="fas fa-dollar-sign"></i>
                            </div>
                            <div>
                                <h3 style="margin: 0; color: #2c3e50;"><?php echo formatPrice($revenue_stats['total_revenue'] ?? 0); ?></h3>
                                <p style="margin: 0; color: #7f8c8d;">Total Revenue</p>
                            </div>
                        </div>
                    </div>
                    <div class="card">
                        <div style="display: flex; align-items: center; gap: 1rem;">
                            <div style="background: #27ae60; color: white; width: 60px; height: 60px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 1.5rem;">
                                <i class="fas fa-shopping-cart"></i>
                            </div>
                            <div>
                                <h3 style="margin: 0; color: #2c3e50;"><?php echo $revenue_stats['total_orders'] ?? 0; ?></h3>
                                <p style="margin: 0; color: #7f8c8d;">Total Orders</p>
                            </div>
                        </div>
                    </div>
                    <div class="card">
                        <div style="display: flex; align-items: center; gap: 1rem;">
                            <div style="background: #3498db; color: white; width: 60px; height: 60px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 1.5rem;">
                                <i class="fas fa-users"></i>
                            </div>
                            <div>
                                <h3 style="margin: 0; color: #2c3e50;"><?php echo $customer_stats['total_customers'] ?? 0; ?></h3>
                                <p style="margin: 0; color: #7f8c8d;">Total Customers</p>
                            </div>
                        </div>
                    </div>
                    <div class="card">
                        <div style="display: flex; align-items: center; gap: 1rem;">
                            <div style="background: #f39c12; color: white; width: 60px; height: 60px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 1.5rem;">
                                <i class="fas fa-chart-line"></i>
                            </div>
                            <div>
                                <h3 style="margin: 0; color: #2c3e50;"><?php echo formatPrice($revenue_stats['avg_order_value'] ?? 0); ?></h3>
                                <p style="margin: 0; color: #7f8c8d;">Avg Order Value</p>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 2rem;">
                    <div class="card">
                        <div class="card-header"><h2>Recent Orders</h2></div>
                        <?php if (empty($recent_orders)): ?>
                            <p style="color: #7f8c8d; text-align: center; padding: 2rem;">No orders yet</p>
                        <?php else: ?>
                            <div class="table-responsive">
                                <table class="table">
                                    <thead><tr><th>Order #</th><th>Customer</th><th>Total</th><th>Status</th><th>Date</th></tr></thead>
                                    <tbody>
                                        <?php foreach ($recent_orders as $order): ?>
                                        <tr>
                                            <td><a href="order_details.php?id=<?php echo $order['id']; ?>" style="color: #e74c3c;"><?php echo htmlspecialchars($order['order_number']); ?></a></td>
                                            <td><?php echo htmlspecialchars($order['first_name'] . ' ' . $order['last_name']); ?></td>
                                            <td><?php echo formatPrice($order['total_amount']); ?></td>
                                            <td><span class="status-badge status-<?php echo $order['status']; ?>"><?php echo ucfirst($order['status']); ?></span></td>
                                            <td><?php echo date('M j, Y', strtotime($order['created_at'])); ?></td>
                                        </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                        <?php endif; ?>
                    </div>
                    <div class="card">
                        <div class="card-header"><h2>Top Selling Products</h2></div>
                        <?php if (empty($top_products)): ?>
                            <p style="color: #7f8c8d; text-align: center; padding: 2rem;">No sales data yet</p>
                        <?php else: ?>
                            <div style="display: flex; flex-direction: column; gap: 1rem;">
                                <?php foreach ($top_products as $index => $product): ?>
                                <div style="display: flex; justify-content: space-between; align-items: center; padding: 1rem; background: #f8f9fa; border-radius: 8px;">
                                    <div>
                                        <div style="font-weight: 500; color: #2c3e50;"><?php echo htmlspecialchars($product['name']); ?></div>
                                        <div style="color: #7f8c8d; font-size: 0.9rem;"><?php echo $product['total_sold']; ?> sold • <?php echo formatPrice($product['total_revenue']); ?> revenue</div>
                                    </div>
                                    <div style="background: #e74c3c; color: white; width: 30px; height: 30px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: bold;">
                                        <?php echo $index + 1; ?>
                                    </div>
                                </div>
                                <?php endforeach; ?>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
                <?php
                break;
        }
        ?>
    </div>
</main>

<?php include '../includes/footer.php'; ?>

